/**
 */
"use strict"

var Router = require('koa-router');
var config = require('../config/config.js');
var debug = require('debug')('pre_router');

debug('router:' + config.http.baseUrl);

var router = new Router({
	prefix: config.http.baseUrl
});

// router.all('/**', function*(next) {
// 	debug('userId=', this.session.userId);
// 	yield next;
// });

/*** all router */
router.post('/**', function*(next) {
	debug('global router post,url=%s', this.url);

	var inParams;
	if (typeof this.request.body === 'string') {
		try {
			inParams = JSON.parse(this.request.body);
		} catch (e) {
			this.body = 'error : input must be json';
			return;
		}
	} else {
		inParams = this.request.body;
	}

	this.request.inParams = inParams;
	yield next;
});

router.put('/**', function*(next) {
	debug('global router post,url=%s', this.url);

	var inParams;
	if (typeof this.request.body === 'string') {
		try {
			inParams = JSON.parse(this.request.body);
		} catch (e) {
			this.body = 'error : input must be json';
			return;
		}
	} else {
		inParams = this.request.body;
	}

	this.request.inParams = inParams;
	yield next;
});

router.get('/**', function*(next) {
	debug('global router get,url=%s', this.url);
	// debug('query=%s', JSON.stringify(this.request.query));
	// debug('params=%s', JSON.stringify(this.params));
	this.request.inParams = this.request.query;
	yield next;
});


module.exports = router.routes();